package org.ciallo.simplemusicplayer.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import org.ciallo.simplemusicplayer.R;
import org.ciallo.simplemusicplayer.data.model.ArtistInfo;

import java.util.Locale;
import java.util.Objects;

/**
 * @author Jukomu
 * @Description: 显示歌手列表的 RecyclerView 适配器
 * @Package: org.ciallo.simplemusicplayer.adapter
 * @Project: SimpleMusicPlayer
 * @name: ArtistAdapter
 * @Date: 2025/6/2-22:05
 * @Filename: ArtistAdapter
 */
public class ArtistAdapter extends ListAdapter<ArtistInfo, ArtistAdapter.ArtistViewHolder> {

    private final OnArtistClickListener listener;

    public interface OnArtistClickListener {
        void onArtistClick(ArtistInfo artistInfo);
    }

    public ArtistAdapter(OnArtistClickListener listener) {
        super(DIFF_CALLBACK);
        this.listener = listener;
    }

    private static final DiffUtil.ItemCallback<ArtistInfo> DIFF_CALLBACK = new DiffUtil.ItemCallback<ArtistInfo>() {
        @Override
        public boolean areItemsTheSame(@NonNull ArtistInfo oldItem, @NonNull ArtistInfo newItem) {
            return oldItem.artistId == newItem.artistId && Objects.equals(oldItem.artistName, newItem.artistName);
        }

        @Override
        public boolean areContentsTheSame(@NonNull ArtistInfo oldItem, @NonNull ArtistInfo newItem) {
            return oldItem.equals(newItem); // ArtistInfo 需要实现 equals
        }
    };

    @NonNull
    @Override
    public ArtistViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 为歌手列表创建一个新的 item 布局，例如 item_artist.xml
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_artist, parent, false);
        return new ArtistViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ArtistViewHolder holder, int position) {
        ArtistInfo artistInfo = getItem(position);
        if (artistInfo != null) {
            holder.bind(artistInfo, listener);
        }
    }

    static class ArtistViewHolder extends RecyclerView.ViewHolder {
        ImageView ivArtistCover; // 可选的歌手代表封面
        TextView tvArtistName;
        TextView tvSongCount;

        ArtistViewHolder(View itemView) {
            super(itemView);
            ivArtistCover = itemView.findViewById(R.id.imageView_item_artist_cover); // 假设 item_artist.xml 中有此ID
            tvArtistName = itemView.findViewById(R.id.textView_item_artist_name);
            tvSongCount = itemView.findViewById(R.id.textView_item_artist_song_count);
        }

        void bind(final ArtistInfo artistInfo, final OnArtistClickListener listener) {
            tvArtistName.setText(artistInfo.artistName);
            tvSongCount.setText(String.format(Locale.getDefault(), "%d首歌曲", artistInfo.songCount));

            Context context = itemView.getContext();
            Glide.with(context)
                    .load(artistInfo.representativeAlbumArtUri != null ? Uri.parse(artistInfo.representativeAlbumArtUri) : null)
                    .placeholder(R.drawable.ic_default_artist_avatar) // 需要一个歌手默认头像
                    .error(R.drawable.ic_default_artist_avatar)
                    .into(ivArtistCover);

            itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onArtistClick(artistInfo);
                }
            });
        }
    }
}
